package BuildTrainSet.AssignTag;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;

/**
 *
 * @author phamthanh
 */
public class NumTag {

//    public static void main(String argv[]) throws XMLStreamException, IOException {
//
//        NumTag nt = new NumTag("trainset//hand//num_ncom_hand.xml", "trainset//hand//num_ncom_assign.xml");
//    }

    public NumTag(String inF, String ouF) throws XMLStreamException, IOException {
        long startTime = System.currentTimeMillis();
        int ntim = 0;
        int ndat = 0;
        int nday = 0;
        int nmon = 0;
        int nnum = 0;
        int ntel = 0;
        int ndig = 0;
        int nscr = 0;
        int nrgn = 0;
        int nfrc = 0;
        int nadd = 0;
        int ncom = 0;

        System.out.println("package BuildTrainSet.AssignTag; Class NumTag");
        
        FileInputStream fis = new FileInputStream(inF);
        XMLStreamReader xmlStreamReader = XMLInputFactory.newInstance().createXMLStreamReader(fis);

        FileOutputStream fos = new FileOutputStream(ouF);
        OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF8");
        XMLStreamWriter xmlStreamWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(osw);

        xmlStreamWriter.writeStartElement("root");
        while (xmlStreamReader.hasNext()) {
            int eventCode = xmlStreamReader.next();
            switch (eventCode) {

                case XMLStreamConstants.START_ELEMENT:
                    String localName = xmlStreamReader.getLocalName();
                    if (localName.equals("record")) {
                        String RecId = xmlStreamReader.getAttributeValue(0);
                        xmlStreamWriter.writeStartElement(localName);
                        xmlStreamWriter.writeAttribute("RecId", RecId);

                    }

                    if (localName.equals("URL")) {
                        String url = xmlStreamReader.getElementText();

                        xmlStreamWriter.writeStartElement(localName);
                        xmlStreamWriter.writeCharacters(url);
                        xmlStreamWriter.writeEndElement();

                    }
                    if (localName.equals("Infomation")) {
                        xmlStreamWriter.writeStartElement(localName);
                    }
                    if (localName.equals("NCOM")) {
                        xmlStreamWriter.writeStartElement(localName);
                    }
                    if (localName.equals("W")) {
                        String attributeName = xmlStreamReader.getAttributeLocalName(0);
                        String attributeValue = xmlStreamReader.getAttributeValue(0);
                        if (!attributeValue.equals("NUMBER")) {
                            String nsw = xmlStreamReader.getElementText();
                            xmlStreamWriter.writeStartElement(localName);
                            xmlStreamWriter.writeAttribute(attributeName, attributeValue);
                            xmlStreamWriter.writeCharacters(nsw);
                            xmlStreamWriter.writeEndElement();

                        } else {
                            String nsw = xmlStreamReader.getElementText();
                            String classNsw = detectNumTag(nsw);
                            xmlStreamWriter.writeStartElement(localName);
                            xmlStreamWriter.writeAttribute("NSW", classNsw);
                            if (localName.equals("NTIM")) {
                                ntim++;
                            }
                            if (classNsw.equals("NDAT")) {
                                ndat++;
                            }
                            if (classNsw.equals("NDAY")) {
                                nday++;
                            }
                            if (classNsw.equals("NMON")) {
                                nmon++;
                            }
                            if (classNsw.equals("NNUM")) {
                                nnum++;
                            }
                            if (classNsw.equals("NTEL")) {
                                ntel++;
                            }
                            if (classNsw.equals("NDIG")) {
                                ndig++;
                            }
                            if (classNsw.equals("NSCR")) {
                                nscr++;
                            }
                            if (classNsw.equals("NRGN")) {
                                nrgn++;
                            }
                            if (classNsw.equals("NADD")) {
                                nadd++;
                            }
                            if (classNsw.equals("NCOM")) {
                                ncom++;
                            }
                            xmlStreamWriter.writeCharacters(nsw);
                            xmlStreamWriter.writeEndElement();
                        }
                    }

                    if (localName.equals("SPLIT")) {
                        xmlStreamWriter.writeStartElement(localName);
                    }
                    break;

                case XMLStreamConstants.END_ELEMENT:
                    String endLocalName = xmlStreamReader.getLocalName();

                    if (endLocalName.equals("record")) {
                        xmlStreamWriter.writeEndElement();
                    }
                    if (endLocalName.equals("Infomation")) {
                        xmlStreamWriter.writeEndElement();
                    }

                    if (endLocalName.equals("SPLIT")) {
                        xmlStreamWriter.writeEndElement();
                    }
                    if (endLocalName.equals("NCOM")) {
                        xmlStreamWriter.writeEndElement();
                    }
                    break;
                case XMLStreamConstants.CHARACTERS:
                    String character = xmlStreamReader.getText();
                    xmlStreamWriter.writeCharacters(character);
                    break;

                default:
                    break;
            }
        }

        xmlStreamWriter.writeEndElement();

        xmlStreamReader.close();
        xmlStreamWriter.close();
        fis.close();
        fos.close();
        long endTime = System.currentTimeMillis();
        long time = endTime - startTime;
        System.out.println("    +nadd: " + nadd);
        System.out.println("    +ncom: " + ncom);
        System.out.println("    +ndat: " + ndat);

        System.out.println("    +nday: " + nday);
        System.out.println("    +ndig: " + ndig);
        System.out.println("    +nfrc: " + nfrc);
        System.out.println("    +nmon: " + nmon);
        System.out.println("    +nnum: " + nnum);
        System.out.println("    +nargn: " + nrgn);
        System.out.println("    +nscr: " + nscr);
        System.out.println("    +ntel: " + ntel);
        System.out.println("    +ntim: " + ntim);
        System.out.println("Thoi gian gan the num la: " + time);
    }

    public String detectNumTag(String nsw) {
        Pattern NTIM = Pattern.compile("^[0-2]?[0-9](:[0-6]?[0-9]){1,2}$");
        Pattern NDAT = Pattern.compile("^([0-3]?[0-9]\\-)?([0-3]?[0-9])[\\-/]([0-1]?[0-9])[\\-/][\\d]{2,4}$");
        Pattern NTEL = Pattern.compile("^[\\(\\+]?[\\d \\(\\)\\.\\-/]{9,}\\d$");
        Pattern NRNG = Pattern.compile("^\\d[\\d.,]*\\p{Pd}\\d[\\d.,]*$");
        Pattern NDAY = Pattern.compile("^([0-3]?[0-9]-)?[0-3]?[0-9]/[0-1]?[0-9]$");
        Pattern NMON = Pattern.compile("^[0-1]?[0-9]/[\\d]{2,4}$");
        Pattern NFRC = Pattern.compile("^[\\d]+/[\\d]+$");
        Pattern NNUM = Pattern.compile("^[\\d\\., ]*\\d$");
        Pattern NADD = Pattern.compile("^([\\d]+/)*[\\d]+$");


        Matcher mTim = NTIM.matcher(nsw);
        Matcher mDat = NDAT.matcher(nsw);
        Matcher mTel = NTEL.matcher(nsw);
        Matcher mRgn = NRNG.matcher(nsw);
        Matcher mDay = NDAY.matcher(nsw);
        Matcher mMon = NMON.matcher(nsw);
        Matcher mFrc = NFRC.matcher(nsw);
        Matcher mAdd = NADD.matcher(nsw);
        Matcher mNum = NNUM.matcher(nsw);

        if (mTim.find()) {
            return "NTIM";
        }

        if (mDat.find()) {
            return "NDAT";
        }

        if (mTel.find()) {
            if ((getNumCount(nsw) == 10) || (getNumCount(nsw) == 11)) {
                return "NTEL";
            }

        }
        if (mRgn.find()) {
            return "NRGN";
        }

        if (mDay.find()) {
            return "NDAY";
        }

        if (mMon.find()) {
            return "NMON";
        }

        if (mFrc.find()) {
            return "NFRC";
        }

        if (mNum.find()) {
            return "NNUM";
        }
        if (mAdd.find()) {
            return "NADD";
        }




        return "NCOM";
    }

    public int getNumCount(String nsw) {
        int count = 0;
        for (int i = 0; i < nsw.length(); i++) {
            if (Character.isDigit(nsw.charAt(i))) {
                count++;
            }
        }
        return count;
    }
}
